package com.jarvan.service.impl;

import com.jarvan.enums.PassengerEnum;
import com.jarvan.model.Passenger;
import com.jarvan.repository.PassengerRepository;
import com.jarvan.service.PassengerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
public class PassengerServiceImpl implements PassengerService {

    @Autowired
    private PassengerRepository repository;

    @Override
    public Passenger save(Passenger passenger) {
        return repository.save(passenger);
    }

    @Override
    public Passenger findByName(String loginName) {
        return repository.findByLoginName(loginName);
    }

    @Override
    public Passenger findById(Integer passengerId) {
        return repository.findById(passengerId).orElse(null);
    }

    @Override
    public Page<Passenger> findAllByPage(Pageable pageable) {
        return repository.findAll(pageable);
    }

    @Override
    public Passenger freenzon(Integer passengerId) {
        Passenger passenger = this.findById(passengerId);
        passenger.setStatus(PassengerEnum.FREEZON_PASSENGER.getCode());
        return repository.save(passenger);
    }

    @Override
    public Passenger toNormal(Integer passengerId) {
        Passenger passenger = this.findById(passengerId);
        passenger.setStatus(PassengerEnum.NORMAL_PASSENGER.getCode());
        return repository.save(passenger);
    }
}
